<template>
  <div>
    <BasicTable @register="registerTable">
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleExport"> 导出 </a-button>
      </template> -->
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              icon: 'humbleicons:bell',
              onClick: handleEdit.bind(null, record),
            },
          ]" />
        </template>
      </template>
    </BasicTable>
    <HandleModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
import { BasicTable, useTable, TableAction } from '@/components/Table'
import { getAlarmRecordModel } from '@/api/alarm/alarm'
import { columns, searchFormSchema } from './gas.data'
import { reactive } from 'vue'
import HandleModal from './HandleModal.vue'
import { useModal } from '@/components/Modal'
import { message } from 'ant-design-vue'

defineOptions({ name: 'GasAlarm' })
const [registerModal, { openModal }] = useModal();
const searchInfo = reactive<Recordable>({ alarmModelId: 1 });
const [registerTable, { reload, updateTableDataRecord }] = useTable({
  title: '气体告警',
  api: getAlarmRecordModel,
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
  },
  searchInfo,
  useSearchForm: true,
  beforeFetch(info) { // 手动处理time属性
    if (info.time) {
      const timeparams = {
        alarmStartTime: info.time[0].toString(),
        alarmEndTime: info.time[1].toString()
      }
      delete info.time;
      return { ...info, ...timeparams }
    }
  },
  showTableSetting: false,
  bordered: true,
  showIndexColumn: true,
  actionColumn: {
    width: 80,
    title: '操作',
    dataIndex: 'action',
    // slots: { customRender: 'action' },
    fixed: undefined,
  },
})

function handleExport() {
  // openDrawer(true, {
  //   isUpdate: false,
  // })
}

function handleEdit(record: Recordable) {
  if (record.processStatus == 0) {
    openModal(true, {
      record,
      isUpdate: true,
    })
  } else {
    message.success('该报警已处理')
  }
}

function handleSuccess({ isUpdate, values }) {
  if (isUpdate) {
    // 演示不刷新表格直接更新内部数据。
    // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
    const result = updateTableDataRecord(values.id, values);
  } else {
    reload();
  }
}

</script>

<style lang="less" scoped>
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(-360deg);
  }
}
</style>
